package jh.jianguan.springboot.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jh.jianguan.springboot.model.User;
import jh.jianguan.springboot.service.impl.UserService;
import jh.jianguan.springboot.util.ResultUtil;
import jh.jianguan.springboot.util.common.exception.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 方法描述
 * @return
 * @author guoc
 * @date 2020/11/5 15:58
 */
@RestController
@Api(value = "测试 swagger2")
@Slf4j
public class UserController {




    @Autowired
    UserService userService;

    /**
     * restfor 传参
     * http://localhost:8089/getuser/3
     * @return jh.jianguan.springboot.model.User
     * @author guoc
     * @date 2020/11/5 16:30
     */
    @RequestMapping(value = "getuser/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "返回用户输入的参数内容", notes = "Swagger测试")
    @ApiImplicitParam(paramType = "path", name = "id", value = "人员ID",required = true, dataType = "Integer")
    public User getUser(@PathVariable int id){
        return userService.getUser(id);
    }

    /**
     * 分页插件
     * http://localhost:8089/list?page=1&pageSize=2
     * @return com.github.pagehelper.PageInfo<jh.jianguan.springboot.model.User>
     * @author guoc
     * @date 2020/11/5 16:31
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "返回用户分页展示", notes = "分页插件接口测试")
    public PageInfo<User> list(@RequestParam(value="pageNo",defaultValue="1")int pageNo, @RequestParam(value="pageSize",defaultValue="10")int pageSize) {
        PageInfo<User> page = userService.getAllUserForPage(pageNo, pageSize);
        return  page;
    }


    /**
     * redis缓冲，工具类测试
     * http://localhost:8089/getUserAddcache/3
     * @return jh.jianguan.springboot.model.User
     * @author guoc
     * @date 2020/11/26 10:14
     */
    @RequestMapping(value = "getUserAddcache/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "redis缓冲，工具类测试", notes = "redis缓冲，工具类测试")
    @ApiImplicitParam(paramType = "path", name = "id", value = "人员ID",required = true, dataType = "Integer")
    public User getUserAddcache(@PathVariable int id){
        return userService.getUserAddcache(id);
    }

    /**
     * post请求，通过JsonObject接参
     * @return jh.jianguan.springboot.model.User
     * @author guoc
     * @date 2020/11/26 15:38
     */
    @RequestMapping(value = "getUserByPost",method = RequestMethod.POST)
    @ApiOperation(value = "post接收参数", notes = "通过JsonObject接参")
    @ApiImplicitParam(paramType = "path", name = "id", value = "人员ID",required = true, dataType = "String")
    public User getUserByPost(@RequestBody JSONObject userJSON){
        String id = userJSON.getString("id");
        return userService.getUserAddcache(Integer.valueOf(id));
    }

    /**
     *  http://localhost:8089/encapsulateReturne
     *  {"id":"3"}
     * @return jh.jianguan.springboot.util.ResultUtil
     * @author guoc
     * @date 2020/11/27 15:04
     */
    @RequestMapping(value = "encapsulateReturne",method = RequestMethod.POST)
    @ApiOperation(value = "封装返回值", notes = "封装返回值")
    @ApiImplicitParam(paramType = "path", name = "id", value = "人员ID",required = true, dataType = "String")
    public ResultUtil encapsulateReturne(@RequestBody JSONObject userJSON){
        String id = userJSON.getString("id");
        return ResultUtil.successJson(userService.getUserAddcache(Integer.valueOf(id)));
    }

    /**
     * http://localhost:8089/exceptionReturne
     * {"id":"2"}
     * @return jh.jianguan.springboot.util.ResultUtil
     * @author guoc
     * @date 2020/11/27 15:30
     */
    @RequestMapping(value = "exceptionReturne",method = RequestMethod.POST)
    @ApiOperation(value = "封装全局异常返回值", notes = "封装全局异常返回值")
    @ApiImplicitParam(paramType = "path", name = "id", value = "人员ID",required = true, dataType = "String")
    public ResultUtil exceptionReturne(@RequestBody JSONObject userJSON){
        String id = userJSON.getString("id");
        if (id.equals("0")){
            int a =1/0;
        }else if (id.equals("1")){
            JSONObject jsonObject = JSONObject.parseObject(id);
        }else if (id.equals("2")){
            throw ExceptionUtil.mxe("名探偵コナン");
        }else if (id.equals("100")){
            try {
                userService.getExceptionMsg(id);
            } catch (Exception e) {
                log.info(e.getMessage());
            }
        }
        return ResultUtil.successJson(userService.getUserAddcache(Integer.valueOf(id)));
    }

}
